package com.p2p.bill.controller;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.p2p.bill.po.Bill;
import com.p2p.bill.service.BillService;
import com.p2p.bill.vo.BillCondition;
import com.p2p.bill.vo.BillSellect;
import com.p2p.bill.vo.BillUpdate;
import com.p2p.common.constants.Message;
import com.p2p.common.enums.ResponseEnum;
import com.p2p.common.utils.ParamUtils;
import com.p2p.daily.po.P2pDailyOutput;
import com.p2p.terminal.vo.IpCondition;

@RestController
@RequestMapping(value = "bill")
public class BillController {

	private static final Logger logger = LoggerFactory.getLogger(BillController.class);
	@Autowired
	private BillService billService;

	@RequestMapping(value = "selectBill", method = RequestMethod.POST)
	public Message selectPortOption(HttpServletRequest request, @RequestBody BillCondition condition) {
		String agentid = condition.getAgentId();
		String date_time = condition.getDate_time();
		System.out.println(date_time);
		String agent = "agentid";

		if (agentid.equals("0")) {

			agentid = "agentid";
		}

		List<BillSellect> portList = billService.getBill(agentid, date_time);
		return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg(), portList);
	}

	@RequestMapping(value = "updateAllBill", method = RequestMethod.POST)
	public Message updateAllBill(HttpServletRequest request, @RequestBody List<BillUpdate> list) {
		List<BillUpdate> billlist = new ArrayList<BillUpdate>();
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getCommission() != null) {
				billlist.add(list.get(i));
			}
		}
		billService.updateAllBill(billlist);
		return new Message();
	}

	@RequestMapping(value = "updateAllBillrelease", method = RequestMethod.POST)
	public Message updateAllBillrelease(HttpServletRequest request, @RequestBody List<BillUpdate> list) {
	
		billService.updateRelease(list);
		return new Message();
	}

	@RequestMapping(value = "updateBill", method = RequestMethod.GET)
	public Message updateBill(HttpServletRequest request) {

		String id = ParamUtils.getParameter(request, "id");
		if (id != null) {
			String commission = ParamUtils.getParameter(request, "commission");
			Bill bill = new Bill();
			bill.setStatus(1);
			bill.setId(Long.parseLong(id));
			bill.setCommission(Integer.valueOf(commission));
			bill.setUpdateTime(new Date());
			boolean flagUpdate = billService.updateById(bill);
			if (flagUpdate) {
				return new Message(200, "更新成功");

			}
			return new Message(500, "更新失败");
		}
		return new Message(500, "更新失败");

	}

}
